//
//  MPMineController.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/17.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "MPMineController.h"
#import "MPMenuCell.h"
#import "MPMinePageController.h"
#import "MPMoreController.h"
#import "MPMoneyController.h"
#import "MPFeedbackController.h"
#import "MPGuideController.h"
#import "MPMenuHeaderView.h"
#import "MPLoginController.h"
#import "MPReceiptController.h"

@interface MPMineController ()
{}
@property (nonatomic, strong) NSMutableArray<MPMenuModel *> *dataList;
@property (nonatomic, strong) MPMenuHeaderView *header;
@end

@implementation MPMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
    [self setupData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupData) name:ATEnterpriseUpdateNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.header.nameLabel.text = [ATLocal user].name;
    self.header.phoneLabel.text = [ATLocal user].tel;
    [self.header.imageView sd_setImageWithURL:[NSURL URLWithEncodeString:[ATLocal user].headPic]];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupData {
    @weakify(self)
    self.dataList = [NSMutableArray array];
    if (![ATLocal isEnterprise]) {
        [self.dataList addObject:[MPMenuModel modelWithTitle:@"我的任务" icon:@"leftbar_icon_renwu" didClick:^{
            @strongify(self)
            [self pushViewController:[[MPMinePageController alloc] init]];
        }]];
    }
    [self.dataList addObject:[MPMenuModel modelWithTitle:@"使用指南" icon:@"leftbar_icon_zhinan" didClick:^{
        @strongify(self)
        [self pushViewController:[[MPGuideController alloc] init]];
    }]];
    [self.dataList addObject:[MPMenuModel modelWithTitle:@"钱包" icon:@"leftbar_icon_qianbao" didClick:^{
        @strongify(self)
        [self pushViewController:([ATLocal isEnterprise] ? [[MPReceiptController alloc] init] : [[MPMoneyController alloc] init])];
    }]];
    [self.dataList addObject:[MPMenuModel modelWithTitle:@"意见反馈" icon:@"leftbar_icon_yijian" didClick:^{
        @strongify(self)
        [self pushViewController:[[MPFeedbackController alloc] init]];
    }]];
    [self.dataList addObject:[MPMenuModel modelWithTitle:@"更多信息" icon:@"leftbar_icon_more" didClick:^{
        @strongify(self)
        [self pushViewController:[[MPMoreController alloc] init]];
    }]];
    [self.dataList addObject:[MPMenuModel modelWithTitle:([ATLocal isEnterprise] ? @"切换个人版" : @"切换企业版") icon:@"leftbar_icon_qiehuan" didClick:^{
        @strongify(self)
        [ATLocal setIsEnterprise:![ATLocal isEnterprise]];
        [self.frostedViewController hideMenuViewController];
    }]];
    
    [self.tableView reloadData];
}

#pragma mark - actions
- (void)pushViewController:(UIViewController *)controller {
    [self.frostedViewController hideMenuViewController];
    [((UINavigationController *)self.frostedViewController.contentViewController) pushViewController:controller animated:YES];
}

- (void)didClickLogout:(id)sender {
    [self.frostedViewController hideMenuViewController];
    [ATLocal loginOut];
    [((UINavigationController *)self.frostedViewController.contentViewController) presentViewController:[[BaseNavigationController alloc] initWithRootViewController:[[MPLoginController alloc] init]] animated:YES completion:nil];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MPMenuCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPMenuCell *cell = [MPMenuCell cellForTableView:tableView indexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    MPMenuModel *model = self.dataList[indexPath.row];
    !model.didClickBlock ?: model.didClickBlock();
}

#pragma mark - setupViews
- (void)setupViews {
    self.at_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = self.header;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.scrollEnabled = NO;
    
    MPAppButton *logoutButton = [MPAppButton buttonWithTitle:@"退出登录" target:self action:@selector(didClickLogout:)];
    [self.view addSubview:logoutButton];
    [logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(logoutButton.superview).offset(15.0f);
        make.centerX.equalTo(logoutButton.superview);
        make.bottom.equalTo(logoutButton.superview).offset(-58 - TAB_BAR_ADDING);
        make.height.equalTo(@(45.0f));
    }];
}

#pragma mark - getter
- (MPMenuHeaderView *)header {
    if (!_header) {
        _header = [MPMenuHeaderView instanceView];
    }
    return _header;
}
@end
